﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CnGine.Comm;
using Comm;

public partial class admin_imageadd : LoginBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindColor();
        }
    }

    public void BindColor()
    {
        BLL.Color b = new BLL.Color();
        drpColor.DataSource = b.GetList(" IsShow=1  order by Ordinal asc");
        drpColor.DataTextField = "ClassName";
        drpColor.DataValueField = "ID";
        drpColor.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string[] allowExt = new string[] { ".jpg", ".png", ".gif", ".bmp" };
        string title = txtTitle.Text.Trim();
        string target = txtTarget.Text.Trim();
        string image = fileupImage.FileName;
        string exten = System.IO.Path.GetExtension(image).ToLower();
        bool isallow = false;
        foreach (string s in allowExt)
        {
            if (exten == s) isallow = true; break;
        }
        if (isallow)
        {
            Model.Img img = new Model.Img();
            img.ColorID = drpColor.SelectedValue;
            img.UserID = base.User.ID;
            img.Title = title;
            img.Tag = target;

            img.Hits = 0;
            img.IsPass = true;
            img.LoveNum = 0;
            img.CommentNum = 0;
            img.AddTime = System.DateTime.Now;
            string unquineName = CnGine.Text.Sequence.GetDateTimeOrder();
            //上传文件
            //string filepath = string.Format("/UploadImage/Image/{0}/{1}b.jpg", base.User.ID, unquineName);
            //new Comm.Dispose_Image().upLoadThumbnail(fileupImage.PostedFile, filepath, 245, 200);
            //filepath = string.Format("/UploadImage/Image/{0}/{1}s.jpg", base.User.ID, unquineName);
            //new Comm.Dispose_Image().upLoadThumbnail(fileupImage.PostedFile, filepath, 127, 100);
            //filepath = string.Format("/UploadImage/Image/{0}/{1}.jpg", base.User.ID, unquineName);
            //new Comm.Dispose_Image().upLoadThumbnail(fileupImage.PostedFile, filepath, 522, 100, "W");
            //img.Pic = filepath;

            string Pic = (new Dispose_Image()).upLoadImage(fileupImage, "/UploadImage/PersonalityImg");
            if (!string.IsNullOrEmpty(Pic))
            {

                img.Pic = (new Dispose_Image()).upLoadThumbnail(Server.MapPath(Pic), string.Format("/UploadImage/PersonalityImg/{0}/Big/", base.User.ID), 245, 200).Replace("//", "/");
                img.Pic = (new Dispose_Image()).upLoadThumbnail(Server.MapPath(Pic), string.Format("/UploadImage/PersonalityImg/{0}/Small/", base.User.ID), 127, 100).Replace("//", "/");
                img.Pic = (new Dispose_Image()).upLoadThumbnail(Server.MapPath(Pic), string.Format("/UploadImage/PersonalityImg/{0}/", base.User.ID), 522, 100, "W").Replace("//", "/");
            }
            //System.IO.File.Delete(Server.MapPath(Pic));

            if (new BLL.Img().Add(img) > 0)
            {
                CnGine.Comm.MessageBox.AlertAndRedirect("添加成功！", "imageadd.aspx", this.Page);
            }
            else
            {
                CnGine.Comm.MessageBox.Alert("添加失败请重试", this.Page);
            }
        }
        else
        {
            CnGine.Comm.MessageBox.Alert("图片格式错误，只允许上传 jpg,png,gif,bmp文件", this);
        }
    }
}